//
//  ViewController.m
//  11-23tableView
//
//  Created by blue on 15-11-23.
//  Copyright (c) 2015年 hyd. All rights reserved.
//

#import "ViewController.h"
#import "inputViewController.h"
@interface ViewController ()<InputFinshDelegate>{

    NSArray * data;
    NSArray * tels;
    NSIndexPath * curIndexPath;
}
@end

@implementation ViewController



- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    self.title = @"我的通讯录";
    data = @[
             @"李易峰",
             @"陈翔",
             @"鹿晗",
             @"陈晓",
             @"陈妍希"
             ];
    
    tels = @[
             @"1324567890",
             @"1524879562",
             @"1365789562",
             @"1358542168",
             @"1822534109"
             ];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    NSUserDefaults * def = [NSUserDefaults standardUserDefaults];//单例模式
   [def setObject:data forKey:@"data"];
   [def setObject:tels forKey:@"tels"];
   [def synchronize];

    data = [def objectForKey:@"data"];
    tels = [def objectForKey:@"tels"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return data.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.textLabel.text = data[indexPath.row];
    cell.detailTextLabel.text = tels[indexPath.row];
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    inputViewController * con = [[inputViewController alloc]initWithStyle:UITableViewStyleGrouped];
    curIndexPath = indexPath;//更改多行记录
    con.tel = tels[indexPath.row];
    con.delegate = self;
    [self.navigationController pushViewController:con animated:YES];

    //tel打电话，sms发信息
//    UIApplication * app = [UIApplication sharedApplication];
//    NSString * tel = [NSString stringWithFormat:@"sms://%@",tels[indexPath.row]];
//   [app openURL:[NSURL URLWithString:tel]];
//
}

- (void)inputFinished:(NSString *)text
{
    NSLog(@"indexPath:%@",[self.tableView indexPathForSelectedRow]);
    NSMutableArray * arr = tels.mutableCopy;
    [arr replaceObjectAtIndex:curIndexPath.row withObject:text];
    tels = arr;
    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
